﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
    public partial class SearchVisitsForSearch
    {
        partial void SearchVisitsForSearch_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            string userName = this.Application.User.Name;

            if (this.Application.User.HasPermission(Permissions.CanEditVisit))//admin
            {
                this.CustomerIdFrom = int.MinValue;
                this.CustomerIdTo = int.MaxValue;
                this.SalesPersonIdFrom = int.MinValue;
                this.SalesPersonIdTo = int.MaxValue;
            }
            else if (this.Application.User.HasPermission(Permissions.CanSearchVisit + Permissions.CanManageMyVisit))
            {
                //sales person
                foreach (SalesPerson item in this.DataWorkspace.ApplicationData.SalesPersons)
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        this.SalesPersonIdFrom = item.Id;
                        this.SalesPersonIdTo = item.Id;
                        break;
                    }
                }

                //customer
                foreach (SalesPerson item in this.DataWorkspace.ApplicationData.Customers.Where(c => c.aspnet_Profile_UserId != null))
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        this.CustomerIdFrom = item.Id;
                        this.CustomerIdTo = item.Id;
                        break;
                    }
                }
            }
        }
    }
}
